﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.DisputeTypes.Authorization;
using LeadThing.AbpZeroTemplate.DisputeTypes.Dtos;
using LeadThing.AbpZeroTemplate.Dto;


namespace LeadThing.AbpZeroTemplate.DisputeTypes
{
    /// <summary>
    /// 纠纷类型服务实现
    /// </summary>
    //[AbpAuthorize(DisputeTypeAppPermissions.DisputeType)]


    public class DisputeTypeAppService : AbpZeroTemplateAppServiceBase, IDisputeTypeAppService
    {
        private readonly IRepository<DisputeType, long> _disputeTypeRepository;
        private readonly IDisputeTypeListExcelExporter _disputeTypeListExcelExporter;


        private readonly DisputeTypeManage _disputeTypeManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public DisputeTypeAppService(IRepository<DisputeType, long> disputeTypeRepository,
DisputeTypeManage disputeTypeManage
      , IDisputeTypeListExcelExporter disputeTypeListExcelExporter
  )
        {
            _disputeTypeRepository = disputeTypeRepository;
            _disputeTypeManage = disputeTypeManage;
            _disputeTypeListExcelExporter = disputeTypeListExcelExporter;
        }


        #region 实体的自定义扩展方法

        private IQueryable<DisputeType> _disputeTypeRepositoryAsNoTrack => _disputeTypeRepository.GetAll().AsNoTracking();

        /// <summary>
        /// 获取所有纠纷分类
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<DisputeTypeListDto>> GetAllDisputeTypeList()
        {
            var query = _disputeTypeRepositoryAsNoTrack;
            var items = await query.ToListAsync();
            var disputeTypeListDto = items.MapTo<List<DisputeTypeListDto>>();

            return new ListResultDto<DisputeTypeListDto>(disputeTypeListDto);
        }

        #endregion


        #region 纠纷类型管理

        /// <summary>
        /// 根据查询条件获取纠纷类型分页列表
        /// </summary>
        public async Task<PagedResultDto<DisputeTypeListDto>> GetPagedDisputeTypesAsync(GetDisputeTypeInput input)
        {

            var query = _disputeTypeRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件
            if (!string.IsNullOrWhiteSpace(input.Filter))
                query = query.Where(p => p.Title.Contains(input.Filter));

            var disputeTypeCount = await query.CountAsync();

            var disputeTypes = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var disputeTypeListDtos = disputeTypes.MapTo<List<DisputeTypeListDto>>();
            return new PagedResultDto<DisputeTypeListDto>(
            disputeTypeCount,
            disputeTypeListDtos
            );
        }

        /// <summary>
        /// 通过Id获取纠纷类型信息进行编辑或修改 
        /// </summary>
        public async Task<GetDisputeTypeForEditOutput> GetDisputeTypeForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetDisputeTypeForEditOutput();

            DisputeTypeEditDto disputeTypeEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _disputeTypeRepository.GetAsync(input.Id.Value);
                disputeTypeEditDto = entity.MapTo<DisputeTypeEditDto>();
            }
            else
            {
                disputeTypeEditDto = new DisputeTypeEditDto();
            }

            output.DisputeType = disputeTypeEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取纠纷类型ListDto信息
        /// </summary>
        public async Task<DisputeTypeListDto> GetDisputeTypeByIdAsync(EntityDto<long> input)
        {
            var entity = await _disputeTypeRepository.GetAsync(input.Id);

            return entity.MapTo<DisputeTypeListDto>();
        }







        /// <summary>
        /// 新增或更改纠纷类型
        /// </summary>
        public async Task CreateOrUpdateDisputeTypeAsync(CreateOrUpdateDisputeTypeInput input)
        {
            if (input.DisputeTypeEditDto.Id.HasValue)
            {
                await UpdateDisputeTypeAsync(input.DisputeTypeEditDto);
            }
            else
            {
                await CreateDisputeTypeAsync(input.DisputeTypeEditDto);
            }
        }

        /// <summary>
        /// 新增纠纷类型
        /// </summary>
        //[AbpAuthorize(DisputeTypeAppPermissions.DisputeType_CreateDisputeType)]
        public virtual async Task<DisputeTypeEditDto> CreateDisputeTypeAsync(DisputeTypeEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<DisputeType>();

            entity = await _disputeTypeRepository.InsertAsync(entity);
            return entity.MapTo<DisputeTypeEditDto>();
        }

        /// <summary>
        /// 编辑纠纷类型
        /// </summary>
        //[AbpAuthorize(DisputeTypeAppPermissions.DisputeType_EditDisputeType)]
        public virtual async Task UpdateDisputeTypeAsync(DisputeTypeEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _disputeTypeRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _disputeTypeRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除纠纷类型
        /// </summary>
        //[AbpAuthorize(DisputeTypeAppPermissions.DisputeType_DeleteDisputeType)]
        public async Task DeleteDisputeTypeAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _disputeTypeRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除纠纷类型
        /// </summary>
        //[AbpAuthorize(DisputeTypeAppPermissions.DisputeType_DeleteDisputeType)]
        public async Task BatchDeleteDisputeTypeAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _disputeTypeRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 纠纷类型的Excel导出功能

        public async Task<FileDto> GetDisputeTypeToExcel()
        {
            var entities = await _disputeTypeRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<DisputeTypeListDto>>();

            var fileDto = _disputeTypeListExcelExporter.ExportDisputeTypeToFile(dtos);

            return fileDto;
        }

        #endregion

    }
}
